package com.oasistudio.tk.connector.imp;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TreeMap;

import org.json.JSONObject;

import com.oasistudio.tk.connector.TopAPIConnectorHelper;
import com.oasistudio.tk.connector.TopApiRequest;
import com.oasistudio.tk.connector.TopApiResponse;
import com.taobao.tbw.common.TBWContext;

public class ShopGetConnector implements TopAPIConnectorHelper {
	private String fields="sid,cid,nick,title,pic_path,shop_score";//�??????��????
	private String nick;//??���??
	public ShopGetConnector() {
	}

	public void setNick(String nick){
		this.nick = nick;
	}

	public void setFields(String fields){
		this.fields = fields;
	}

	@Override
	public Object asyncPaser(byte[] part) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getApiUrl() {
		TopApiRequest request = new TopApiRequest();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar calendar = Calendar.getInstance();
		TreeMap<String, String> params = new TreeMap<String, String>();
		//???系�????
		params.put("method", "taobao.shop.get");
		//params.put("session", TBWContext.getInstance().getTopsession());
		params.put("timestamp", sdf.format(calendar.getTime()));
		params.put("format", "json");
		params.put("app_key", TBWContext.getInstance().getAppKey());
		params.put("v", "2.0");
		params.put("partner_id", "top-apitools");
		params.put("sign_method", "md5");
		//API method ???
		params.put("fields", fields);
		params.put("nick", nick);
		request.params = params;
		return request.generalRequestUrl(true);
	}

	/**
	 * �??�?????�???��? total_results Number ??��?��?交�?信�??��? trades Trade[] 交�?信�???��
	 * hash_next Boolean ??��???�?��
	 */
	@Override
	public Object syncPaser(byte[] all) {
		ShopInfo shopInfo = new ShopInfo();
		try {
			TopApiResponse response = new TopApiResponse("shop_get_response");
			String str = new String(all, "UTF-8");
			//Log.d("return msg:", str);
			if (response.parseResult(str).success) {
				shopInfo.isSuccess = true;
				JSONObject jsObj = response.data;
				if (jsObj.has("shop")) {
					JSONObject shopJSON = jsObj.getJSONObject("shop");
					Shop shop = new Shop();
					shopInfo.shop = shop;
					shop.sid = shopJSON.optLong("sid");
					shop.title = shopJSON.optString("title");
					shop.bulletin = shopJSON.optString("bulletin");
					shop.cid = shopJSON.optLong("cid");
					shop.created = shopJSON.optString("created");
					shop.desc = shopJSON.optString("desc");
					shop.modified = shopJSON.optString("modified");
					shop.nick = shopJSON.optString("nick");
					shop.pic_path = "http://logo.taobao.com/shop-logo"+shopJSON.optString("pic_path");
					shop.shop_score = shopJSON.optString("shop_score");
				}
			} else {
				shopInfo.isSuccess = false;
				shopInfo.errCode = response.errCode;
				shopInfo.errInfo = response.errInfo;
			}

		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return shopInfo;
	}

	public static class ShopInfo {
		public String errCode;
		public String errInfo;
		public Boolean isSuccess;
		public Shop shop;
	}

	public static class Shop {
		public Number sid;
		public Number cid;
		public String nick;
		public String title;
		public String desc;
		public String bulletin;
		public String pic_path;
		public String created;
		public String modified;
		public String shop_score;
		
		public String toString(){
			StringBuilder sb = new StringBuilder();
			sb.append("sid:"+sid);
			sb.append(";nick:"+nick);
			sb.append(";title:"+title);
			sb.append(";pic_path:"+pic_path);
			return sb.toString();
		}
	}
}
